package com.uphone.kingmall.fragment;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.activity.GoodsDetailActivity;
import com.uphone.kingmall.adapter.ShopAllGoodsAdapter;
import com.uphone.kingmall.adapter.ShopGoodsCatsAdapter;
import com.uphone.kingmall.base.BaseFragment;
import com.uphone.kingmall.bean.CatsListBean;
import com.uphone.kingmall.bean.GoodsListBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.ToastUtils;
import com.uphone.kingmall.view.MyListView;

import butterknife.BindView;

/**
 * Created by tzh on 2020/6/11.
 */
public class ShopAllGoodsFragment extends BaseFragment {
    @BindView(R.id.lv_type)
    MyListView lv_Type;
    @BindView(R.id.lv_goods)
    MyListView lv_goods;
    @BindView(R.id.tv_empty)
    TextView tv_empty;
    private int id;
    private ShopAllGoodsAdapter adapter_goods;
    private ShopGoodsCatsAdapter adapter_cats;

    public static ShopAllGoodsFragment newInstance(int id) {
        Bundle args = new Bundle();
        ShopAllGoodsFragment fragment = new ShopAllGoodsFragment();
        fragment.id = id;
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_fragment_shop_all_goods;
    }

    @Override
    public void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getShopGoodsCats();
    }

    @Override
    public void initView(View view) {
        adapter_cats = new ShopGoodsCatsAdapter(getContext());
        lv_Type.setAdapter(adapter_cats);
        adapter_goods = new ShopAllGoodsAdapter(getActivity());
        lv_goods.setAdapter(adapter_goods);
        lv_Type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter_cats.selectItem = position;
                adapter_cats.notifyDataSetChanged();
                getGoodsByShopCat(adapter_cats.getItem(position).getId());
            }
        });
        lv_goods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CommonUtil.startIntent(getActivity(), GoodsDetailActivity.class, adapter_goods.getItem(position).getGoodsId());
            }
        });
        adapter_goods.setAddGoodsListener(new ShopAllGoodsAdapter.AddGoodsListener() {
            @Override
            public void onAddClick(GoodsListBean.DataBean bean) {
                addShoppingCart(bean);
            }
        });
    }

    /**
     * 查询商铺分类
     */
    private void getShopGoodsCats() {
        HttpParams params = new HttpParams();
        params.put("shopId", id);
        OkGoUtils.normalRequest(MyUrl.getShopGoodsCats, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                CatsListBean bean = GsonUtils.getGson().fromJson(body, CatsListBean.class);
                if (bean.getCode() == 0 && bean.getCats() != null && bean.getCats().size() > 0) {
                    adapter_cats.setData(bean.getCats());
                    adapter_cats.notifyDataSetChanged();
                    getGoodsByShopCat(bean.getCats().get(adapter_cats.selectItem).getId());
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    private void getGoodsByShopCat(int catId) {
        HttpParams params = new HttpParams();
        params.put("catId", catId);
        params.put("limit", 100);
        params.put("page", 1);
        params.put("shopId", id);
        OkGoUtils.normalRequest(MyUrl.getGoodsByShopCat, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                GoodsListBean bean = GsonUtils.getGson().fromJson(body, GoodsListBean.class);
                if (bean.getCode() == 0 && bean.getData() != null && bean.getData().size() > 0) {
                    tv_empty.setVisibility(View.GONE);
                    adapter_goods.setData(bean.getData());
                    adapter_goods.notifyDataSetChanged();
                    ViewGroup.LayoutParams params = lv_goods.getLayoutParams();
                    params.height = SizeUtils.dp2px(110) * bean.getData().size()
                            + (lv_goods.getDividerHeight() * (bean.getData().size() - 1));
                    lv_goods.setLayoutParams(params);
                } else {
                    tv_empty.setVisibility(View.VISIBLE);
                    adapter_goods.clearData();
                    adapter_goods.notifyDataSetChanged();
                    lv_goods.getLayoutParams().height = 0;
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    private void addShoppingCart(final GoodsListBean.DataBean dataBean) {
        HttpParams params = new HttpParams();
        params.put("goodsId", dataBean.getGoodsId());
        if (dataBean.getProps() != null) {
            String goodsPropIds = "";
            for (int i = 0; i < dataBean.getProps().size(); i++) {
                for (int j = 0; j < dataBean.getProps().get(i).getProp().size(); j++) {
                    goodsPropIds += dataBean.getProps().get(i).getProp().get(j).getGoodsPropId() + ",";
                }
            }
            if (!TextUtils.isEmpty(goodsPropIds)) {
                String str = goodsPropIds.substring(0, goodsPropIds.length() - 1);
                params.put("goodsPropIds", str);
            }
        }
        params.put("goodsNum", 1);
        OkGoUtils.normalRequest(MyUrl.addShopCar, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String response, int code) {
                if (code == 0) {
                    ToastUtils.showShortToast(getContext(), "成功加入购物车！");
                    //点击添加按钮，刷新商品类别布局和底部购物车布局
                    if (mListener != null) mListener.onAddClick();
                    getShopGoodsCats();
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    private AddGoodsListener mListener;

    public interface AddGoodsListener {
        void onAddClick();
    }

    public void setAddGoodsListener(AddGoodsListener mListener) {
        this.mListener = mListener;
    }
}
